package com.verisette.ncos.persistence.dao;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.persistence.dao.CommonDAOImpl;

import com.verisette.ncos.persistence.domain.MasterUserGroup;

/**
 * 
 * {Description here}
 *
 * @author Ratapong Wongpiang (Earth)
 * @since Jul 1, 2010 11:04:34 AM
 * @see
 */
public class RoleManagementDAOImpl extends CommonDAOImpl implements RoleManagementDAO {

	private Logger logger = Logger.getLogger(RoleManagementDAOImpl.class);
	
	@Override
	public List<MasterUserGroup> inquiryUserGroup(MasterUserGroup userGroup, PagingBean pagingBean) {
		List param = new LinkedList();
		List<MasterUserGroup> userGroupList = null;
		StringBuffer q1 = new StringBuffer();
		
		q1.append("SELECT * FROM MASTER_USER_GROUP");
		
		if(userGroup != null && userGroup.getUsgName() != null && !"".equals(userGroup.getUsgName())){
			q1.append(" WHERE USG_NAME like ?");
			param.add("%"+userGroup.getUsgName()+"%");
		}
		
		q1.append(" order by USG_NAME ");
		q1.append(pagingBean.getOrderMode()); //add by pimonrath (kae) 08/11/2010
		
		userGroupList = (List<MasterUserGroup>)nativeQuery(q1.toString(), MasterUserGroup.class, pagingBean, param.toArray());
			
		return userGroupList;
	}

	@Override
	public void updateUserGroup(MasterUserGroup userGroup, String updateBy, Date currentDate) {
		List param = new LinkedList();
		StringBuffer q1 = new StringBuffer();
		
		q1.append("UPDATE MASTER_USER_GROUP");
		q1.append(" SET USG_NAME = ?, USG_STATUS = ?, USG_LAST_UPDATED_DATE = ? , USG_LAST_UPDATED_BY = ?");
		q1.append(" WHERE USG_ID = ?");
		
		
		param.add(userGroup.getUsgName());
		param.add(userGroup.getUsgStatus());
		param.add(new Timestamp(currentDate.getTime()));
		param.add(updateBy);
		param.add(userGroup.getUsgId());
		
		executeNativeSQL(q1.toString(), param.toArray());
		
	}

	@Override
	public boolean findUserGroupName(MasterUserGroup userGroup) {
		// For Edit UserGroup
		boolean foundUserGroupName = false;
		List param = new LinkedList();
		List<MasterUserGroup> userGroupList = null;
		StringBuffer q1 = new StringBuffer();
		
		q1.append("SELECT * FROM MASTER_USER_GROUP WHERE USG_ID <> ? and USG_NAME = ?");
		
		param.add(userGroup.getUsgId());
		param.add(userGroup.getUsgName());
		
		userGroupList = (List<MasterUserGroup>)nativeQuery(q1.toString(), MasterUserGroup.class, param.toArray());
		
		if(userGroupList != null && userGroupList.size() != 0)
			foundUserGroupName = true;
		
		return foundUserGroupName;
	}

}
